package com.dz.core.constant;

import java.util.HashMap;
import java.util.Map;

/**
 * 请求响应码枚举
 * Created by lether on 2016/7/28.
 */
public enum ResCode {

    /**
     * "1000","成功"
     */
    SUCCESS(1000,"成功"),
    /**
     * "1001","业务失败"
     */
    FAIL(1001,"业务失败"),
    /**
     * "1002","权限不足"
     */
    PERMISSION_DENIED(1002,"权限不足"),
    /**
     * "1003","无效token"
     */
    INVALID_TOKEN(1003,"无效token"),

    /**
     * "1004","token过期"
     */
    TOKEN_EXPIRATION(1004,"token过期"),


    /**
     * "1005","token丢失"
     */
    TOKEN_LOSE(1005,"token丢失"),

    /**
     * "1006","签名无效"
     */
    INVALID_SIGNATURE(1006,"签名无效"),

    /**
     * "1007","参数丢失"
     */
    PARAMETER_MISS(1007,"参数丢失"),
    /**
     * "1008","参数格式错误"
     */
    PARAMETER_TYPE_MISMATCHING(1008,"参数格式错误"),

    /**
     * "1009","非法请求"
     */
    ILLEGAL(1009,"非法请求"),

    /**
     * "1100","重复提交"
     */
    REPEAT_SUBMIT(1100,"重复提交"),

    NOT_FOUND(404,"找不到资源"),
    SERVER_ERROR(500,"服务器错误");


    private int key;
    private String value;
    /**
     * getKey
     * @return String getKey
     */
    public int getKey() {
        return key;
    }
    /**
     * getValue
     * @return String getValue
     */
    public String getValue() {
        return value;
    }

    ResCode(int key, String value) {
        this.key = key;
        this.value = value;
    }

    private static Map<Integer, ResCode> valueMap = new HashMap<Integer, ResCode>();

    static {
        for (ResCode _enum : ResCode.values()) {
            valueMap.put(_enum.key, _enum);
        }
    }

    /**
     * 判定给定的key是当前枚举所定义的枚举值类型，如果不定义，返回假.
     *
     * @param typeKey 待判定的交易类型key
     * @return boolean 如果给定的类型值typeKey在当前枚举里定义，则返回true；否则返回false
     */
    public static boolean isValid(String typeKey) {
        if (typeKey == null) {
            return false;
        }
        return valueMap.containsKey(typeKey);
    }

    /**
     * 方法描述: 枚举转换
     *
     * @param key key
     * @return ResCode ResCode
     */
    public static ResCode parseOf(int key) {
        for (ResCode item : values()) {
            if (item.getKey() == key) {
                return item;
            }
        }
        return null;
    }

    public static ResCode parseOf(String key) {
        return ResCode.parseOf(Integer.parseInt(key));
    }
}
